package com.qcby.controller;
import com.qcby.entity.*;
import com.qcby.service.*;
//import com.qcby.entity.AdmStudent;
//import com.qcby.entity.Admin;
//import com.qcby.entity.Admteacher;
//import com.qcby.entity.Edu;
//import com.qcby.service.AdmStudentService;
//import com.qcby.service.AdmTeacherService;
//import com.qcby.service.AdminService;
//import com.qcby.service.EduService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author 阿笙
 */
@Controller
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private AdmStudentService admStudentService;
    @Autowired
    private AdmTeacherService admTeacherService;
    @Autowired
    private EduService eduService;
    //跳转到管理员登录界面
    @RequestMapping("adiLogin")
    public String adiLogin(){

        return "adiLogin";
    }
    @RequestMapping("AdminLogin")
    public String AdminLogin(Admin admin, Model model){
        List<Admin> admins = adminService.find(admin);
        if(admins != null && !admins.isEmpty()){
            List<Admin> listAdmin = adminService.listAdmin();
            model.addAttribute("admin",listAdmin);
            //成功进入主页面
            return "admin";
        }else{
            //想写一个弹出窗
            return "adiLogin";
        }
    }

    @RequestMapping("admin")
    public String admin(){

        return "admin";
    }
    @RequestMapping("listAdi")
    public String adi(Model model){
        List<Admin> adminList = adminService.listAdmin();
        model.addAttribute("admin",adminList);
        return "listAdi";
    }
    @RequestMapping("listStu")
    public String stu(Model model){
        List<AdmStudent> studentList = admStudentService.findStudent();
        model.addAttribute("student",studentList);
        return "listStu";
    }
    @RequestMapping("listTea")
    public String tea(Model model){
        List<Admteacher> teacherList = admTeacherService.findTeacher();
        model.addAttribute("teacher",teacherList);
        return "listTea";
    }
    @RequestMapping("listEdu")
    public String edu(Model model){
        List<Edu> eduList = eduService.listEdu();
        model.addAttribute("edu",eduList);
        return "listEdu";
    }
    @RequestMapping("addSelect")
    public String addSelect(){

        return "addSelect";
    }
    @RequestMapping("deleteId")
    public String daleteId(Integer id){
        adminService.deleteId(id);
        return "redirect:listAdi";
    }
    @RequestMapping("deleteStu")
    public String deleteStu(Integer id){
        admStudentService.deleteStu(id);
        return "redirect:listStu";
    }
    @RequestMapping("deleteTea")
    public String deleteTea(Integer id){
        admTeacherService.deleteTea(id);
        return "redirect:listTea";
    }
    @RequestMapping("deleteEdu")
    public String deleteEdu(Integer id){
        eduService.deleteEdu(id);
        return "redirect:listEdu";
    }
    @RequestMapping("stuFind")
    public String stuFind(String student,Model model){
        List<AdmStudent> stuFind = admStudentService.stuFind(student);
        if(stuFind.isEmpty()){
            model.addAttribute("msg",1);
            List<AdmStudent> studentList = admStudentService.findStudent();
            model.addAttribute("student",studentList);
            return "listStu";
        }
        model.addAttribute("student",stuFind);
        return "listStu";
    }
    @RequestMapping("teaFind")
    public String teaFind(String teacher,Model model){
        List<Admteacher> teaFind = admTeacherService.teaFind(teacher);
        System.out.println(teaFind);
        if(teaFind.isEmpty()){
            model.addAttribute("msg",1);
            List<Admteacher> teacherList = admTeacherService.findTeacher();
            model.addAttribute("teacher",teacherList);
            return "listTea";
        }
        model.addAttribute("teacher",teaFind);
        return "listTea";
    }
    @RequestMapping("adiFind")
    public String adiFind(String admin,Model model){
        List<Admin> adiFind = adminService.adiFind(admin);
        System.out.println(adiFind);
        if(adiFind.isEmpty()){
            model.addAttribute("msg",1);
            List<Admin> adminList = adminService.adiFind(admin);
            model.addAttribute("admin",adminList);
            return "listAdi";
        }
        model.addAttribute("admin",adiFind);
        return "listAdi";
    }
    @RequestMapping("eduFind")
    public String eduFind(String edu,Model model){
        List<Edu> eduFind = eduService.eduFind(edu);
        System.out.println(eduFind);
        if(eduFind.isEmpty()){
            model.addAttribute("msg",1);
            List<Edu> eduList = eduService.eduFind(edu);
            model.addAttribute("edu",eduList);
            return "listEdu";
        }
        model.addAttribute("edu",eduFind);
        return "listEdu";
    }

    @RequestMapping("addTea")
    public String addTea(){
        return "addTea";
    }
    @RequestMapping("addEdu")
    public String addEdu(){
        return "addEdu";
    }
    @RequestMapping("addAdi")
    public String addAdi(){
        return "addAdi";
    }
    //修改+增加
    @RequestMapping("updateStu")
    public String updateStu(Integer id, Model model, AdmStudent student){
        System.out.println("---------------------"+student);
        System.out.println("ID"+student.getId());
        if(student.getId()==null){//用于添加
            int a = admStudentService.insert(student);
            if(a==1){
                System.out.println("******");
                model.addAttribute("msg",2);
                return "addStu";
            }else {
                model.addAttribute("msg",3);
                return "addStu";
            }
        }else {
            System.out.println("进入");
            List<AdmStudent> students = admStudentService.selectById(id);
            model.addAttribute("student",students.get(0));
            if(student.getStu_name()==null){//用于修改
                return "addStu";
            }else{
                System.out.println("进入修改方法");
                int i = admStudentService.updateByPrimaryKey(student);
                if(i==0){
                    model.addAttribute("msg",4);
                    return "addStu";
                }
                else {
                    model.addAttribute("msg",5);
                    List<AdmStudent> studentList = admStudentService.findStudent();
                    model.addAttribute("student",studentList);
                    return "listStu";
                }
            }
        }
    }
    @RequestMapping("updateTea")
    public String updateTea(Integer id, Model model, Admteacher teacher){
        System.out.println(teacher+"---------------------");
        if(teacher.getId()==null){//用于添加
            int a = admTeacherService.insert(teacher);
            if(a==1){
                System.out.println("******");
                model.addAttribute("msg",2);
                return "addTea";
            }else {
                model.addAttribute("msg",3);
                return "addTea";
            }
        }else {
            List<Admteacher> teachers = admTeacherService.selectById(id);
            model.addAttribute("teacher",teachers.get(0));
            if(teacher.getTea_name()==null){//用于修改
                return "addTea";
            }else{
                int i = admTeacherService.updateByPrimaryKey(teacher);
                if(i==0){
                    model.addAttribute("msg",4);
                    return "addTea";
                }
                else {
                    model.addAttribute("msg",5);
                    List<Admteacher> teacherList = admTeacherService.findTeacher();
                    model.addAttribute("teacher",teacherList);
                    return "listTea";
                }
            }
        }
    }
    @RequestMapping("updateEdu")
    public String updateEdu(Integer id,Model model,Edu edu){
        System.out.println("---------------------"+edu.getId());
        if(edu.getId()==null){//用于添加
            int a = eduService.insert(edu);
            if(a==1){
                System.out.println("******");
                model.addAttribute("msg",2);
                return "addEdu";
            }else {
                model.addAttribute("msg",3);
                return "addEdu";
            }
        }else {
            System.out.println("11111111");
            List<Edu> edus = eduService.selectById(id);
            model.addAttribute("edu",edus.get(0));
            if(edu.getEdu_name()==null){//用于修改
                return "addEdu";
            }else{
                System.out.println("222222进修改里面了");
                int i = eduService.updateByPrimaryKey(edu);
                if(i==0){
                    model.addAttribute("msg",4);
                    return "addEdu";
                }
                else {
                    model.addAttribute("msg",5);
                    List<Edu> eduList = eduService.listEdu();
                    model.addAttribute("edu",eduList);
                    return "listEdu";
                }
            }
        }
    }
    @RequestMapping("updateAdi")
    public String updateAdi(Integer id,Model model,Admin admin){
        System.out.println(admin+"---------------------");
        if(admin.getId()==null){//用于添加
            int a = adminService.insert(admin);
            if(a==1){
                System.out.println("******");
                model.addAttribute("msg",2);
                return "addAdi";
            }else {
                model.addAttribute("msg",3);
                return "addAdi";
            }
        }else {
            List<Admin> admins = adminService.selectById(id);
            model.addAttribute("admin",admins.get(0));
            if(admin.getAdi_name()==null){//用于修改
                return "addAdi";
            }else{
                int i = adminService.updateByPrimaryKey(admin);
                if(i==0){
                    model.addAttribute("msg",4);
                    return "addAdi";
                }
                else {
                    model.addAttribute("msg",5);
                    List<Admin> adminList = adminService.listAdmin();
                    model.addAttribute("admin",adminList);
                    return "listAdi";
                }
            }
        }
    }
}
